﻿namespace PerpetuumSoft.InstrumentationPDA
{
    using PerpetuumSoft.InstrumentationPDA.Mathematics;
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;

    public class Dial : CircularControl
    {
        private Image dialImage;
        private Color externalCircleBorderColor = Color.Black;
        private Color externalCircleColor = Color.SlateGray;
        private int externalCircleRadius = 0x2d;
        private PerpetuumSoft.InstrumentationPDA.ImagePosition imagePosition = PerpetuumSoft.InstrumentationPDA.ImagePosition.DialCenter;
        private Color imageTransparentColor = Color.Transparent;
        private Color internalCircleBorderColor = Color.Transparent;
        private Color internalCircleColor = Color.LightGray;
        private int internalCircleRadius = 0x19;
        private Color lightCircleColor = Color.Gainsboro;
        private Color notchesColor = Color.Black;
        private int notchesCount = 5;
        private bool showNotches = true;

        protected override void DrawDynamic(Graphics g)
        {
            if (this.ShowNotches)
            {
                for (int i = 0; i < this.NotchesCount; i++)
                {
                    decimal angle = ((360M / this.NotchesCount) * i) + base.ValueToAngle(base.Value);
                    Vector vector = base.ToPoint(base.AngleToValue(angle), this.internalCircleRadius);
                    Vector vector2 = base.ToPoint(base.AngleToValue(angle), this.externalCircleRadius);
                    g.DrawLine(new Pen(this.NotchesColor), base.Round(vector.X), base.Round(vector.Y), base.Round(vector2.X), base.Round(vector2.Y));
                }
            }
            base.DrawDynamic(g);
        }

        protected override void DrawStatic(Graphics g)
        {
            if (this.dialImage != null)
            {
                Rectangle rectangle;
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorKey(this.ImageTransparentColor, this.ImageTransparentColor);
                switch (this.ImagePosition)
                {
                    case PerpetuumSoft.InstrumentationPDA.ImagePosition.Stretch:
                        g.DrawImage(this.dialImage, this.WorkRectangle, 0, 0, this.dialImage.Width, this.dialImage.Height, GraphicsUnit.Pixel, imageAttr);
                        goto Label_06D7;

                    case PerpetuumSoft.InstrumentationPDA.ImagePosition.Normal:
                        g.DrawImage(this.dialImage, this.WorkRectangle, this.WorkRectangle.X, this.WorkRectangle.Y, this.WorkRectangle.Width, this.WorkRectangle.Height, GraphicsUnit.Pixel, imageAttr);
                        goto Label_06D7;

                    case PerpetuumSoft.InstrumentationPDA.ImagePosition.ControlCenter:
                        rectangle = new Rectangle(base.Round((this.WorkRectangle.X + (this.WorkRectangle.Width / 2)) - (this.dialImage.Width / 2)), base.Round((this.WorkRectangle.Y + (this.WorkRectangle.Height / 2)) - (this.dialImage.Height / 2)), this.dialImage.Width, this.dialImage.Height);
                        g.DrawImage(this.dialImage, rectangle, 0, 0, this.dialImage.Width, this.dialImage.Height, GraphicsUnit.Pixel, imageAttr);
                        goto Label_06D7;

                    case PerpetuumSoft.InstrumentationPDA.ImagePosition.DialCenter:
                        rectangle = new Rectangle(base.Round(base.CenterPoint.X - (this.dialImage.Width / 2)), base.Round(base.CenterPoint.Y - (this.dialImage.Height / 2)), this.dialImage.Width, this.dialImage.Height);
                        g.DrawImage(this.dialImage, rectangle, 0, 0, this.dialImage.Width, this.dialImage.Height, GraphicsUnit.Pixel, imageAttr);
                        goto Label_06D7;
                }
            }
            else
            {
                if (this.externalCircleColor != Color.Transparent)
                {
                    g.FillEllipse(new SolidBrush(this.externalCircleColor), new VectorRectangle(base.CenterPoint.X - this.externalCircleRadius, base.CenterPoint.Y - this.externalCircleRadius, 2 * this.externalCircleRadius, 2 * this.externalCircleRadius).Truncate());
                }
                if (this.externalCircleBorderColor != Color.Transparent)
                {
                    g.DrawEllipse(new Pen(this.externalCircleBorderColor), new VectorRectangle(base.CenterPoint.X - this.externalCircleRadius, base.CenterPoint.Y - this.externalCircleRadius, 2 * this.externalCircleRadius, 2 * this.externalCircleRadius).Truncate());
                }
                Pen pen = new Pen(this.lightCircleColor);
                decimal num = 120M;
                decimal num2 = 3M;
                int num3 = 0;
                while (num3 < 10)
                {
                    Vector vector = Vector.FromPolar(this.InternalCircleRadius, num * 0.0174532925199433M);
                    Vector vector2 = Vector.FromPolar(this.ExternalCircleRadius, num * 0.0174532925199433M);
                    vector.X += base.CenterPoint.X;
                    vector.Y = base.CenterPoint.Y - vector.Y;
                    vector2.X += base.CenterPoint.X;
                    vector2.Y = base.CenterPoint.Y - vector2.Y;
                    g.DrawLine(pen, base.Round(vector.X), base.Round(vector.Y), base.Round(vector2.X), base.Round(vector2.Y));
                    vector = Vector.FromPolar(this.InternalCircleRadius, (num + 180M) * 0.0174532925199433M);
                    vector2 = Vector.FromPolar(this.ExternalCircleRadius, (num + 180M) * 0.0174532925199433M);
                    vector.X += base.CenterPoint.X;
                    vector.Y = base.CenterPoint.Y - vector.Y;
                    vector2.X += base.CenterPoint.X;
                    vector2.Y = base.CenterPoint.Y - vector2.Y;
                    g.DrawLine(pen, base.Round(vector.X), base.Round(vector.Y), base.Round(vector2.X), base.Round(vector2.Y));
                    num3++;
                    num += num2;
                }
                if (this.internalCircleColor != Color.Transparent)
                {
                    g.FillEllipse(new SolidBrush(this.internalCircleColor), new VectorRectangle(base.CenterPoint.X - this.internalCircleRadius, base.CenterPoint.Y - this.internalCircleRadius, 2 * this.internalCircleRadius, 2 * this.internalCircleRadius).Truncate());
                }
                if (this.internalCircleBorderColor != Color.Transparent)
                {
                    g.DrawEllipse(new Pen(this.internalCircleBorderColor), new VectorRectangle(base.CenterPoint.X - this.internalCircleRadius, base.CenterPoint.Y - this.internalCircleRadius, 2 * this.internalCircleRadius, 2 * this.internalCircleRadius).Truncate());
                }
            }
        Label_06D7:
            base.DrawStatic(g);
        }

        protected override Size DefaultControlSize
        {
            get
            {
                return new Size(200, 200);
            }
        }

        public Image DialImage
        {
            get
            {
                return this.dialImage;
            }
            set
            {
                if (this.dialImage != value)
                {
                    this.dialImage = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color ExternalCircleBorderColor
        {
            get
            {
                return this.externalCircleBorderColor;
            }
            set
            {
                if (this.externalCircleBorderColor != value)
                {
                    this.externalCircleBorderColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color ExternalCircleColor
        {
            get
            {
                return this.externalCircleColor;
            }
            set
            {
                if (this.externalCircleColor != value)
                {
                    this.externalCircleColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public int ExternalCircleRadius
        {
            get
            {
                return this.externalCircleRadius;
            }
            set
            {
                if (this.externalCircleRadius != value)
                {
                    this.externalCircleRadius = value;
                    base.InvalidateStatic();
                }
            }
        }

        public PerpetuumSoft.InstrumentationPDA.ImagePosition ImagePosition
        {
            get
            {
                return this.imagePosition;
            }
            set
            {
                if (this.imagePosition != value)
                {
                    this.imagePosition = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color ImageTransparentColor
        {
            get
            {
                return this.imageTransparentColor;
            }
            set
            {
                if (this.imageTransparentColor != value)
                {
                    this.imageTransparentColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color InternalCircleBorderColor
        {
            get
            {
                return this.internalCircleBorderColor;
            }
            set
            {
                if (this.internalCircleBorderColor != value)
                {
                    this.internalCircleBorderColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color InternalCircleColor
        {
            get
            {
                return this.internalCircleColor;
            }
            set
            {
                if (this.internalCircleColor != value)
                {
                    this.internalCircleColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public int InternalCircleRadius
        {
            get
            {
                return this.internalCircleRadius;
            }
            set
            {
                if (this.internalCircleRadius != value)
                {
                    this.internalCircleRadius = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color LightCircleColor
        {
            get
            {
                return this.lightCircleColor;
            }
            set
            {
                if (this.lightCircleColor != value)
                {
                    this.lightCircleColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color NotchesColor
        {
            get
            {
                return this.notchesColor;
            }
            set
            {
                if (this.notchesColor != value)
                {
                    this.notchesColor = value;
                    base.Invalidate();
                }
            }
        }

        public int NotchesCount
        {
            get
            {
                return this.notchesCount;
            }
            set
            {
                if (this.notchesCount != value)
                {
                    this.notchesCount = value;
                    base.Invalidate();
                }
            }
        }

        public bool ShowNotches
        {
            get
            {
                return this.showNotches;
            }
            set
            {
                if (this.showNotches != value)
                {
                    this.showNotches = value;
                    base.Invalidate();
                }
            }
        }
    }
}

